<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Zookeeper事务日志，快照 | flyingzc's blog</title><meta name="author" content="flyingzc"><meta name="copyright" content="flyingzc"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="ZKDataBase结构ZKDataBase(zk内存数据库)    – sessionWithTimeouts(zk所有会话  会话超时时间记录器)    – DataTree存储    – 事务日志 ZKDatabase会定时向磁盘dump快照数据,在zk启动时通过磁盘上的事务日志 和 快照文件 恢复 一个完整的内存数据库 事务日志dataLogDir 文件大小都是64MB 文件名 log.x">
<meta property="og:type" content="article">
<meta property="og:title" content="Zookeeper事务日志，快照">
<meta property="og:url" content="https://flyingzc.github.io/articles/zk/zk06_snapshot_txnlog.html">
<meta property="og:site_name" content="flyingzc&#39;s blog">
<meta property="og:description" content="ZKDataBase结构ZKDataBase(zk内存数据库)    – sessionWithTimeouts(zk所有会话  会话超时时间记录器)    – DataTree存储    – 事务日志 ZKDatabase会定时向磁盘dump快照数据,在zk启动时通过磁盘上的事务日志 和 快照文件 恢复 一个完整的内存数据库 事务日志dataLogDir 文件大小都是64MB 文件名 log.x">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://flyingzc.github.io/images/zc-icon.jpeg">
<meta property="article:published_time" content="2019-05-06T16:00:00.000Z">
<meta property="article:modified_time" content="2025-01-21T01:51:41.216Z">
<meta property="article:author" content="flyingzc">
<meta property="article:tag" content="Zookeeper">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://flyingzc.github.io/images/zc-icon.jpeg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "Zookeeper事务日志，快照",
  "url": "https://flyingzc.github.io/articles/zk/zk06_snapshot_txnlog.html",
  "image": "https://flyingzc.github.io/images/zc-icon.jpeg",
  "datePublished": "2019-05-06T16:00:00.000Z",
  "dateModified": "2025-01-21T01:51:41.216Z",
  "author": [
    {
      "@type": "Person",
      "name": "flyingzc",
      "url": "https://flyingzc.github.io/"
    }
  ]
}</script><link rel="shortcut icon" href="/images/zc-icon.jpeg"><link rel="canonical" href="https://flyingzc.github.io/articles/zk/zk06_snapshot_txnlog.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="DentpX3LnP_WNgEeFnNIcTZZky-Bby-zL-uI_9GqTAw"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({
  google_ad_client: '',
  enable_page_level_ads: 'true'
});</script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Zookeeper事务日志，快照',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/self/github-dark.css"><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">flyingzc's blog</span></a><a class="nav-page-title" href="/"><span class="site-name">Zookeeper事务日志，快照</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">Zookeeper事务日志，快照</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-05-06T16:00:00.000Z" title="发表于 2019-05-07 00:00:00">2019-05-07</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-01-21T01:51:41.216Z" title="更新于 2025-01-21 09:51:41">2025-01-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Zookeeper/">Zookeeper</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h2 id="ZKDataBase结构"><a href="#ZKDataBase结构" class="headerlink" title="ZKDataBase结构"></a>ZKDataBase结构</h2><p>ZKDataBase(zk内存数据库)<br>    – sessionWithTimeouts(zk所有会话  会话超时时间记录器)<br>    – DataTree存储<br>    – 事务日志</p>
<p>ZKDatabase会定时向磁盘dump快照数据,<br>在zk启动时通过磁盘上的事务日志 和 快照文件 恢复 一个完整的内存数据库</p>
<h2 id="事务日志"><a href="#事务日志" class="headerlink" title="事务日志"></a>事务日志</h2><p>dataLogDir</p>
<p>文件大小都是64MB</p>
<p>文件名 log.xxx<br>xxx为事务id : zxid, 且 <code>为写入该事务文件第一条事务记录的zxid</code>.</p>
<p>zxid由两部分组成: 高32位 为 当前leader周期(epoch), 低32位 为 真正的操作序列号.</p>
<p>LogFormatter 可以查看该文件</p>
<h3 id="事务日志操作"><a href="#事务日志操作" class="headerlink" title="事务日志操作"></a>事务日志操作</h3><ol>
<li><p>滚动日志<br>FileTxnLog.rollLog()<br>将当前日志文件 滚动为新文件.<br>其实就是将 logStream置为null,那么要写入日志时就会新创建一个日志文件用于写入.</p>
</li>
<li><p>写事务日志<br>FileTxnLog.append(TxnHeader hdr, Record txn)</p>
</li>
</ol>
<p>若logStream &#x3D;&#x3D; null, 代表要新创建一个事务日志文件.则根据根据事务头里的 zxid创建事务文件名.<br>创建事务日志文件头信息(魔数,版本,dbId),序列化成fileheader,刷新到磁盘.</p>
<p>填充文件.</p>
<p>将 事务头 和 事务数据 序列化成 Byte Buffer,生成一个验证算法,将序列化的事务记录写入OutputArchive.</p>
<h3 id="事务日志写入时机"><a href="#事务日志写入时机" class="headerlink" title="事务日志写入时机"></a>事务日志写入时机</h3><p>SyncRequestProcessor.run()里面调用</p>
<h3 id="事务日志文件的磁盘空间预分配"><a href="#事务日志文件的磁盘空间预分配" class="headerlink" title="事务日志文件的磁盘空间预分配"></a>事务日志文件的磁盘空间预分配</h3><p>事务日志文件大小默认均为64MB,文件大小是预分配的,用”0”填充的,每个事务日志都会及时记录到里面,相当于修改这部分文件.</p>
<p>这样做的好处:</p>
<p>对于客户端的每一次事务操作,zk都会将其写入事务日志文件中.</p>
<p>事务日志的写入性能直接决定了zk事务请求的响应.</p>
<p>事务写入近似可以被看做是一个磁盘I&#x2F;O的过程.</p>
<p>文件的不断追加写入操作会触发底层磁盘I&#x2F;O为文件开辟新的磁盘块,即磁盘seek.</p>
<p>为了避免磁盘Seek的频率,提高磁盘I&#x2F;O效率.zk创建事务日志的时候就会进行文件空间预分配,默认64MB.</p>
<p>一旦已分配的文件空间不足4kb,会再次预分配,以避免随着每次事务的写入过程中文件大小增长带来的seek开销,直至创建新的事务日志.</p>
<p>事务日志”预分配”的大小可以通过系统属性 <code>zookeeper.preAllocSize</code> 来设置.</p>
<p>根据序列化产生的字节数组来计算Checksum,保证事务文件的完整性 和 准确性.</p>
<p>写入流 -&gt; 最后将流刷盘.</p>
<h2 id="snapshot"><a href="#snapshot" class="headerlink" title="snapshot"></a>snapshot</h2><p>snapshot 用于记录zk服务器上某个时刻的<code>全量内存数据</code>,并将其写入到指定的磁盘文件中.</p>
<p>dataDir<br>文件名 snapshot.xxxx<br>每个快照数据文件中的所有内容都是有效的,不会预分配.</p>
<p>SnapshotFormatter 可以查看该文件,该类仅输出每个数据节点的元信息,不输出节点数据内容.</p>
<p>事务操作,zk会记录到事务日志,并且将数据变更应用到内存数据库中.</p>
<p>zk会在进行若干次事务日志记录后,将内存数据库的全量数据 Dump 到本地文件中,即 数据快照.<br>可<code>配置zk在snapCount次事务日志记录后</code>进行一个数据快照.</p>
<h3 id="何时进行快照"><a href="#何时进行快照" class="headerlink" title="何时进行快照"></a>何时进行快照</h3><p>逻辑在 SyncRequestProcessor 类中.<br>logCount &gt; (snapCount &#x2F; 2 + randRool)<br>如 snapCount 默认配置为<code>10,0000</code> ,zk会在 5,0000 - 10,0000 次事务后进行一次数据快照. </p>
<p>随机数避免集群中所有机器在同一时刻进行数据快照.</p>
<p>snapshot 操作单独在一个异步线程处理,将所有<code>DataTree</code>和<code>会话信息</code>保存到本地磁盘中.</p>
<p>根据当前已提交的 最大ZXID 来生成数据快照文件名.</p>
<p>数据序列化:<br>文件头: 魔数, 版本号, dbid<br>会话信息, DataTree, Checksum</p>
<h2 id="初始化时将-snapshot-和-txnlog-应用到内存数据库"><a href="#初始化时将-snapshot-和-txnlog-应用到内存数据库" class="headerlink" title="初始化时将 snapshot 和 txnlog 应用到内存数据库"></a>初始化时将 snapshot 和 txnlog 应用到内存数据库</h2><p>zk 启动时,会根据 snapshot 和 txnlog 恢复内存数据库.</p>
<p>默认会获取磁盘上最新的100个快照文件,但是时逐个解析的,只有最新的文件校验不通过时,才会解析之前的(相当于 <code>使用最新并可用的那个快照文件</code>).</p>
<p>根据快照文件名 获取 zxid 即 zxid_for_snap.</p>
<p>处理事务日志.<code>从事务日志中获取所有比 zxid_for_snap 大的事务操作</code>.</p>
<p>将事务应用到 ZKDatabase 和 sessionWithTimeouts 中.</p>
<p>zk每应用一个事务日志,会回调 PlayBackListener 监听器,将这个事务操作转换成Proposal,保存到 ZKDatabase.committedLog 中,以便启动选举出 leader 后 follower 同步 leader 的数据.</p>
<p>zxid : zookeeper事务id<br>epoch : 标识当前leader周期,每次选举产生一个新的leader服务器后,都会生产一个新的epoch.</p>
<h2 id="snapshot-txnlog-如何保证全量数据"><a href="#snapshot-txnlog-如何保证全量数据" class="headerlink" title="snapshot + txnlog 如何保证全量数据"></a>snapshot + txnlog 如何保证全量数据</h2><p>一个全量的内存数据库.它包含</p>
<ol>
<li>一个最新的可用的快照(假设它的文件名为zxid_for_snap)</li>
<li>事务id大于等于该 zxid_for_snap 的所有事务日志</li>
<li>事务id小于该 zxid_for_snap 的那个最新的事务日志(比如在往这个事务文件里写事务日志,写着写着进行了快照操作,但是由于这个事务日志还没写满,所以会接着往这里面写事务日志)</li>
</ol>
<h2 id="配置项-zookeeper-snapCount"><a href="#配置项-zookeeper-snapCount" class="headerlink" title="配置项 zookeeper.snapCount"></a>配置项 zookeeper.snapCount</h2><p>zookeeper.snapCount 必须 &gt;&#x3D; 2,若小于2会被置为2,默认不配置则取 10,0000</p>
<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10/dist/style.css"><script src="https://fastly.jsdelivr.net/npm/d3@7"></script><script src="https://fastly.jsdelivr.net/npm/markmap-view@0.18.10"></script><script src="https://fastly.jsdelivr.net/npm/markmap-toolbar@0.18.10"></script>
<link rel="stylesheet" href="/css/markmap.css">

<script src="/js/markmap.js"></script>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io">flyingzc</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://flyingzc.github.io/articles/zk/zk06_snapshot_txnlog.html">https://flyingzc.github.io/articles/zk/zk06_snapshot_txnlog.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://flyingzc.github.io" target="_blank">flyingzc's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Zookeeper/">Zookeeper</a></div><div class="post-share"><div class="social-share" data-image="/images/zc-icon.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/articles/zk/zk01_build.html" title="Zookeeper环境搭建"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-17</div><div class="info-item-2">Zookeeper环境搭建</div></div><div class="info-2"><div class="info-item-1">zk idea 代码阅读环境搭建采用最新的 zookeeper release 版本 3.4.13zk发行版下载地址 从 github 上 clone 该版本的代码. 1. ant 构建 zkbuild.xml 搜索 ant-eclipse-1.0.bin.tar.bz2, 1290行修改 12&lt;get src=&quot;http://ufpr.dl.sourceforge.net/project/ant-eclipse/ant-eclipse/1.0/ant-eclipse-1.0.bin.tar.bz2&quot;dest=&quot;$&#123;src.dir&#125;/java/ant-eclipse-1.0.bin.tar.bz2&quot; usetimestamp=&quot;false&quot; /&gt; 执行 ant eclipse 2. idea 导入这个 eclipse 项目即可File -&gt; New -&gt; import project from exist source -&gt; 选eclipse 将...</div></div></div></a><a class="pagination-related" href="/articles/zk/zk02_start.html" title="Zookeeper启动流程"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-18</div><div class="info-item-2">Zookeeper启动流程</div></div><div class="info-2"><div class="info-item-1">zk启动流程 启动类为 QuorumPeerMain 解析 zoo.cfg 配置 创建并启动 DatadirCleanupManager 用于清理过期 snapshot 和 txnlog. 创建 QuorumPeer 实例并启动该线程,用于完成选举. 根据 snapshot 和 txnlog 恢复 内存数据库 ZKDatabase.  12345678910111213141516171819202122232425262728293031323334353637383940- QuorumPeerMain.main()- 1.创建QuorumPeerMain对象- 2.初始化 运行 main.initializeAndRun(args)    - 0.创建QuorumPeerConfig对象    - 1.通过QuorumPeerConfig.parse(),解析配置文件         - 读取zoo.cfg配置到Properties中        - parseProperties()解析Properties配置到QuorumPeerConfig对象中    -...</div></div></div></a><a class="pagination-related" href="/articles/zk/zk03_vote.html" title="Zookeeper选举流程"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-02-18</div><div class="info-item-2">Zookeeper选举流程</div></div><div class="info-2"><div class="info-item-1">leader选举相关线程QuorumCnxManager : 管理者,包含发送 和 接收 队列.持有 listener. QuorumCnxManager.Listener : 监听 并 建立 server 之间的连接. FastLeaderElection.Messenger.SendWorker消息生产者.循环 从发送队列 queueSendMap 中取消息(出队)并发送出去.并将该消息存入 lastMessageSent 中. FastLeaderElection.Messenger.RecvWorker消息消费者.循环 接收线程从底层 Socket 收到报文后放到 recvQueue 队列中,等待 Messenger 调用 pollRecvQueue 方法获取消息. leader选举流程启动时选举选举算法的创建之前先创建 QuorumCnxManager,它通过 TCP 协议来进行 leader...</div></div></div></a><a class="pagination-related" href="/articles/zk/zk04_sync.html" title="Zookeeper数据同步流程"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-01-17</div><div class="info-item-2">Zookeeper数据同步流程</div></div><div class="info-2"><div class="info-item-1">zk选举完后数据同步流程Leader为每个Follower&#x2F;observer都建立一个TCP长连接. LearnerHandler,即learner服务器的管理者,负责follower&#x2F;observer服务器和leader服务器之间的一系列网络通信.包括数据同步,请求转发和Proposal提议的投票等. leader.lead();-&gt;leader 从磁盘中恢复数据和session列表Leader zk.loadData();-&gt;Leader 启动监听线程 LearnerCnxAcceptor, 等待新的followers的连接请求-&gt;follower 连接leaderFollower follower.followLeader();-&gt;LearnerCnxAcceptor 监听到follower的socket请求,为每个 follower 创建单独的 LearnerHandler 线程用于交互.-&gt;follower 发送 FOLLOWERINFO 包 给leader,包括新的zxid​和 sidFollower...</div></div></div></a><a class="pagination-related" href="/articles/zk/zk05_processor.html" title="Zookeeper Processor"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-08</div><div class="info-item-2">Zookeeper Processor</div></div><div class="info-2"><div class="info-item-1">processors PrepRequestProcessor通常是请求处理链的第一个处理器.识别客户端请求是否是事务请求.对于事务请求,做预处理,诸如 创建请求事务头,事务体,会话检查,ACL检查 和 版本检查 等.  ProposalRequestProcessorleader 服务器的事务投票处理器,也是 leader 服务器事务处理流程的发起者.对于 非事务请求,它会直接将请求流转到 CommitProcessor 处理器,不再做其他处理.对于 事务请求,除了将请求交给CommitProcessor处理器外,还会根据请求类型创建对应的 Proposal 提议,并 发给所有 Follower 来发起一次集群内的事务投票.它还会将事务请求交给 SyncRequestProcessor 进行事务日志的记录.  SyncRequestProcessor事务日志记录处理器,将事务请求记录到事务日志文件中,同时还会触发zk进行数据快照.发送Sync请求的处理器.将请求记录到磁盘.它批量处理有效执行io的请求.在将日志同步到磁盘之前,请求不会传递到下一个...</div></div></div></a><a class="pagination-related" href="/articles/zk/zk07_usage.html" title="Zookeeper应用"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2019-05-07</div><div class="info-item-2">Zookeeper应用</div></div><div class="info-2"><div class="info-item-1">dubbo中zk的应用123456789在zk中创建的节点路径dubbo    com.alibaba.demo.DemoService [服务名,service]        configurators        consumers [类型,type,生产者消费者]            ...        providers            ... [url]        routers     其中接口(service)下会有四个子节点providers, consumers, routers, configurators 其中dubbo、接口、providers都是持久化节点，只有url是临时节点。当会话消失(服务器断开与zookeeper的连接)，对应的临时节点会被删除。 dubbo-registry-zookeeper模块中, ZookeeperRegistry 类继承自 FailbackRegistry.构造方法中通过...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/images/zc-icon.jpeg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">flyingzc</div><div class="author-info-description">不积跬步无以至千里<br/>不积小流无以成江海</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">84</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">36</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">29</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/FlyingZC"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/FlyingZC" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">FlyingZC's Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#ZKDataBase%E7%BB%93%E6%9E%84"><span class="toc-number">1.</span> <span class="toc-text">ZKDataBase结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E5%8A%A1%E6%97%A5%E5%BF%97"><span class="toc-number">2.</span> <span class="toc-text">事务日志</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E5%8A%A1%E6%97%A5%E5%BF%97%E6%93%8D%E4%BD%9C"><span class="toc-number">2.1.</span> <span class="toc-text">事务日志操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E5%8A%A1%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%97%B6%E6%9C%BA"><span class="toc-number">2.2.</span> <span class="toc-text">事务日志写入时机</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8B%E5%8A%A1%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6%E7%9A%84%E7%A3%81%E7%9B%98%E7%A9%BA%E9%97%B4%E9%A2%84%E5%88%86%E9%85%8D"><span class="toc-number">2.3.</span> <span class="toc-text">事务日志文件的磁盘空间预分配</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#snapshot"><span class="toc-number">3.</span> <span class="toc-text">snapshot</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%95%E6%97%B6%E8%BF%9B%E8%A1%8C%E5%BF%AB%E7%85%A7"><span class="toc-number">3.1.</span> <span class="toc-text">何时进行快照</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E6%97%B6%E5%B0%86-snapshot-%E5%92%8C-txnlog-%E5%BA%94%E7%94%A8%E5%88%B0%E5%86%85%E5%AD%98%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">4.</span> <span class="toc-text">初始化时将 snapshot 和 txnlog 应用到内存数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#snapshot-txnlog-%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E5%85%A8%E9%87%8F%E6%95%B0%E6%8D%AE"><span class="toc-number">5.</span> <span class="toc-text">snapshot + txnlog 如何保证全量数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E9%A1%B9-zookeeper-snapCount"><span class="toc-number">6.</span> <span class="toc-text">配置项 zookeeper.snapCount</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite03-transaction-begin.html" title="Apache Ignite 开启事务执行流程">Apache Ignite 开启事务执行流程</a><time datetime="2025-10-10T16:00:00.000Z" title="发表于 2025-10-11 00:00:00">2025-10-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite02-transaction.html" title="Apache Ignite 分布式事务原理">Apache Ignite 分布式事务原理</a><time datetime="2025-09-17T16:00:00.000Z" title="发表于 2025-09-18 00:00:00">2025-09-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/ignite/ignite01-build.html" title="Apache Ignite IDEA 环境搭建">Apache Ignite IDEA 环境搭建</a><time datetime="2025-09-10T16:00:00.000Z" title="发表于 2025-09-11 00:00:00">2025-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/druid/druid01-resultset-leak.html" title="ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析">ShardingSphere JDBC 使用 Druid 连接池 ResultSet 内存泄露分析</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/articles/jmeter/jmeter01-shardingsphere.html" title="Jmeter 压测 ShardingSphere">Jmeter 压测 ShardingSphere</a><time datetime="2025-06-30T16:00:00.000Z" title="发表于 2025-07-01 00:00:00">2025-07-01</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2025 By flyingzc</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>